///////////////////////////////////////////////////////////
//  Choices.cpp
//  Implementation of the Class Choices
//  Created on:      19-Aug-2013 10:19:49 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "Choices.h"
#include <algorithm>
#include "DataSchemaXML.h"
#include <sstream>

using namespace Data;

Choices::Choices()
{
  this->m_type = IObject::CHOICES_T;
}


Choices::Choices(const Choices& anotherChoices)
{
  this->m_type = IObject::CHOICES_T;
  this->m_choiceDatas = anotherChoices.m_choiceDatas;
}


Choices::~Choices()
{
  this->m_choiceDatas.clear();
}


IObject::ObjectType Choices::GetType() const 
{
	return this->m_type;
}


void Choices::AddChoice(const String_t& name)
{
  this->m_choiceDatas.push_back(name);
}


String_t Choices::GetChoice(int index) const 
{
  if(index == -1)
    return _T("");
  else
	  return this->m_choiceDatas[index];
}


int Choices::CountChoices() const 
{
	return (int)this->m_choiceDatas.size();
}


void Choices::RemoveChoice(unsigned int index)
{
  this->m_choiceDatas.erase(this->m_choiceDatas.begin() + index);
}

bool Data::Choices::IsChoiceExist( const String_t& name ) const
{
  if(std::find(this->m_choiceDatas.begin(), this->m_choiceDatas.end(), name) != this->m_choiceDatas.end())
    return true;
  else
    return false;
}

String_t Data::Choices::operator[]( unsigned int index )
{
  return this->m_choiceDatas[index];
}

IObject* Choices::Clone()
{
  Choices* cloneChoices = new Choices(*this);
	return  cloneChoices;
}


String_t Choices::ToXML()
{
  //<Choices>
  //  <Choice index="0">Yes</Choice>
  //  <Choice index="1">No</Choice>
  //  <Choice index="2">Cancel</Choice>
  //</Choices>

  String_t xml = _T("<") + DataSchemaXML::CHOICES_ELEMENT + _T(">");
  std::vector<String_t>::iterator choice = this->m_choiceDatas.begin();
  int choiceIndex = 0;
  String_t strContext;

  while(choice != this->m_choiceDatas.end())
  {
    StringStream_t xmlStream;
    strContext = (*choice);
    replaceTextXML(strContext);
    xmlStream << _T("<") << DataSchemaXML::CHOICE_ELEMENT << _T(" ");
    xmlStream << DataSchemaXML::INDEX_ATTR << _T("=\"") << choiceIndex << _T("\">");
    xmlStream << strContext << _T("</") << DataSchemaXML::CHOICE_ELEMENT << _T(">");
    xml += xmlStream.str();
    choice++;
    choiceIndex++;
  }

  xml += _T("</") + DataSchemaXML::CHOICES_ELEMENT + _T(">");
	return xml;
}


